//
//  StarInfomationViewController.m
//  LiveStar
//
//  Created by 漪珊 on 2016/12/15.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "StarInfomationViewController.h"
#import "SendPresentViewController.h"
#import "SendRedPaperController.h"
#import "MessageViewController.h"
#import "StarPredictViewController.h"
#import "StarVideoViewController.h"
#import "StarCooperationViewController.h"
#import "PersonalListViewController.h"
#import "GuardRankViewController.h"
#import "PrivateLetterViewController.h"
#import "LeaveWordViewController.h"
#import "StarFileViewController.h"
#import "FStarTrendViewController.h"
#import "StarDetailView.h"
#import "StarPredictCell.h"
#import "StarInfoGuardCell.h"
#import "StarInfoNormalCell.h"
#import "StarInfoBottomView.h"

@interface StarInfomationViewController ()<UITableViewDelegate,UITableViewDataSource,StarDetailViewDelegate,StarInfoBottomViewDelegate>{
    NSMutableDictionary *_starInfoDict;
    NSMutableDictionary *_starStatusDict;
    NSMutableArray *_userImgArray;
}
@property (nonatomic, strong) StarDetailView *topView;
@property (strong, nonatomic) UITableView *tableView;
/**
   是否关注
 */
@property (nonatomic, assign) BOOL is_Focused;
@end

@implementation StarInfomationViewController

- (void)FansStarHomeRequest {
    NSDictionary *dict = @{@"udid":User_ID,
                           @"token":TOKEN,
                           @"suid":self.s_uid
                           };
    NSString *urlString = [TEST_URL stringByAppendingString:kFStarHome];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 200) {
            _starStatusDict = responseObject[@"data"];
            _starInfoDict = responseObject[@"data"][@"star"];
            NSArray *imgArray = _starStatusDict[@"guard"][@"list"];
            for (int i = 0; i<imgArray.count; i++) {
                NSDictionary *userDict = imgArray[i];
                [_userImgArray addObject:userDict[@"u_avatar"]];
                if (i == 3) break;
            }
            [self.tableView reloadData];
            [self setUpData];
        }
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}

- (void)setUpData {
   [self.topView.backImage sd_setImageWithURL:[NSURL URLWithString:_starInfoDict[@"s_image"]] placeholderImage:nil];
    [self.topView.starImgView sd_setImageWithURL:[NSURL URLWithString:_starInfoDict[@"u_avatar"]] placeholderImage:nil];
    self.topView.starName.text = _starInfoDict[@"s_name"];
    self.topView.focusedNum.text = [NSString stringWithFormat:@"被关注:%@",_starInfoDict[@"s_booked"]];
    self.topView.popularity.text = [NSString stringWithFormat:@"人气:%@",_starInfoDict[@"u_obtain"]];
    self.topView.liveID.text = [NSString stringWithFormat:@" LSID : %@ ",_starInfoDict[@"s_udid"]];
    self.topView.predictNums.text = KString(_starStatusDict[@"advance"]);
    self.topView.videoNums.text = KString(_starStatusDict[@"playBack"]);
    self.topView.trendNums.text = KString(_starStatusDict[@"circle"]);
    if ([_starStatusDict[@"live"] intValue]) {
        self.topView.liveStateBtn.hidden = NO;
    } else {
        self.topView.liveStateBtn.hidden = YES;
    }
    self.topView.starTag1.text = [NSString stringWithFormat:@" %@ ",_starInfoDict[@"s_musictype"]];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self FansStarHomeRequest];
    [self setUpUI];
    self.is_Focused = YES;

}

#pragma mark -- UI
- (void)setUpUI {
    _starInfoDict = [NSMutableDictionary dictionaryWithCapacity:1];
    _starStatusDict = [NSMutableDictionary dictionaryWithCapacity:1];
    _userImgArray = [NSMutableArray arrayWithCapacity:1];
    self.view.backgroundColor = [UIColor grayColor];
    //tableView
    [self creatTableView];
    [self creatBottomView];
    
}

- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tableView.frame = CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT-51);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    StarDetailView *topView = [[NSBundle mainBundle] loadNibNamed:@"StarDetailView" owner:self options:nil].lastObject;
    self.topView = topView;
    topView.delegate = self;
    self.tableView.tableHeaderView = topView;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = kLightGrayBack;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    topView.backgroundColor = kLightGrayBack;
    [self.view addSubview:self.tableView];
}

- (void)creatBottomView {
    StarInfoBottomView *bottomView = [[NSBundle mainBundle] loadNibNamed:@"StarInfoBottomView" owner:self options:nil].lastObject;
    bottomView.delegate = self;
    bottomView.frame = CGRectMake(0, kSCREEN_HEIGHT - 51, kSCREEN_WIDTH, 51);
    [self.view addSubview:bottomView];
    if ([_starInfoDict[@"booked"] intValue] == 10) {//已关注
        [bottomView.focusBtn setTitle:@"已关注" forState:UIControlStateNormal];
    } else {//未关注
        [bottomView.focusBtn setTitle:@"+关注" forState:UIControlStateNormal];
    }
}
#pragma mark -- tableView 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (0 == indexPath.row) {
       StarInfoGuardCell *cell = [StarInfoGuardCell loadNibCellWithTabelView:tableView];
        cell.popularNumsLabel.text = KString(_starStatusDict[@"guard"][@"num"]);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.dataArray = _userImgArray;
        return cell;
    }
    
    StarInfoNormalCell *cell = [StarInfoNormalCell loadNibCellWithTabelView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = UIFont(14);
    if (1 == indexPath.row) {
        cell.titleLabel.text = @"发出的红包";
    } else if (2 == indexPath.row){
        cell.titleLabel.text = @"送出的礼品";
    } else if (3 == indexPath.row){
        cell.titleLabel.text = @"明星小档案";
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {//守护排行榜
        GuardRankViewController *rankVC = [[GuardRankViewController alloc] init];
        rankVC.s_uid = self.s_uid;
        [self.navigationController pushViewController:rankVC animated:YES];
    } else if (indexPath.row == 1) {//发出的红包
        SendRedPaperController *paperVc = [SendRedPaperController new];
        paperVc.suid = self.s_uid;
        [self.navigationController pushViewController:paperVc animated:YES];
    } else if (indexPath.row == 2) {//送出的礼品
        SendPresentViewController *presentVc = [SendPresentViewController new];
        presentVc.suid = self.s_uid;
        [self.navigationController pushViewController:presentVc animated:YES];
    } else if (indexPath.row == 3) {//明星小档案
        StarFileViewController *fileVC = [[StarFileViewController alloc] init];
        fileVC.name = _starInfoDict[@"s_name"];
        fileVC.userType = UserTypeFans;
        fileVC.s_uid = self.s_uid;
        [self.navigationController pushViewController:fileVC animated:YES];
    }
}

#pragma mark -- 区头视图

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 10)];
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 10)];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

#pragma mark -- StarDetailViewDelegate
- (void)popController {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)ItemClickWithTag:(NSUInteger)tag {
    switch (tag) {
        case 1:{//预告
            PersonalListViewController *listVC = [[PersonalListViewController alloc] init];
            listVC.s_uid = self.s_uid;
            listVC.listType = PredictListType;
            [self.navigationController pushViewController:listVC animated:YES];
        }
            break;
        case 2:{//回放
            PersonalListViewController *listVC = [[PersonalListViewController alloc] init];
            listVC.s_uid = self.s_uid;
            listVC.listType = VideoListType;
            [self.navigationController pushViewController:listVC animated:YES];
        }
            break;
        case 3:{//动态
            FStarTrendViewController *trendVC = [[FStarTrendViewController alloc] init];
            trendVC.suid = self.s_uid;
            [self.navigationController pushViewController:trendVC animated:YES];
        }
            break;
            
        default:
            break;
    }
}

#pragma mark -- StarInfoBottomViewDelege
- (void)bottomToolBarActionWithTag:(NSUInteger)tag BottomView:(StarInfoBottomView *)bottomView {
    switch (tag) {
        case 1:{//留言
            LeaveWordViewController *leaveWordVC = [[LeaveWordViewController alloc] init];
            leaveWordVC.s_uid = self.s_uid;
            [self.navigationController pushViewController:leaveWordVC animated:YES];
        }
            break;
        case 2:{//关注
            self.is_Focused = !self.is_Focused;
            if (self.is_Focused) {
                [bottomView.focusBtn setTitle:@"已关注" forState:UIControlStateNormal];
            } else {
                [bottomView.focusBtn setTitle:@"+ 关注" forState:UIControlStateNormal];
            }
        }
            break;
        case 3:{//私信
            PrivateLetterViewController *privateVC = [[PrivateLetterViewController alloc] init];
            privateVC.suid = self.s_uid;
            [self.navigationController pushViewController:privateVC animated:YES];
        }
            break;

            
        default:
            break;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
